import { Button, Form } from 'antd';
import { observable, action, toJS } from 'mobx';
import { WeaSearchGroup, WeaLocaleProvider } from 'ecCom';
import * as smsRegularApi from '../../apis/smsRegular';
import { WeaForm, WeaSwitch } from 'comsMobx';
import { WeaTableNew } from 'comsMobx';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export default class SmsManageStore {
	@observable comsWeaTableStore = new TableStore();
	@observable regularTableStore = new TableStore();
	@observable regularDataKey = '0';
	// 用于控制传给weatable的sessionkey
    @observable dataKey = '0';

    @observable loading = false;

	// 高级搜索的form
    @observable form = new WeaForm();

	// 用于控制高级搜索的显隐的变量
    @observable ifShowSearchAd = false;


	@observable searchKey = '';

    // 控制新建或添加的弹窗的显隐
    @observable ifShowAddDialog = false;

	@observable regularSmsVisible = false;

	@observable regularMemberListVisible = false;

	@observable regularMemberInfoVisible = false;

    // 查看详细时会用到的id
	@observable regularSmsId = '';

	@observable regularMemberId = '';

	@observable msg = '';
	@observable sendDateTime = '';
	@observable sendHrm = [];
	@observable sendCrm = [];
	@observable sendStatus = '0';
	@observable receiverType = '0';
	@observable messageStatus = '0';
	@observable receiverNumber = '';

	@action
    setSearchKey = (key) => {
        this.searchKey = key;
	}

    @action
    setRegularSmsId = (id) => {
        this.regularSmsId = id;
	}

	@action
    setRegularMemberId = (id) => {
        this.regularMemberId = id;
    }

    // 这里拿到的bool是切换显隐前的显隐的bool值
    @action
    setShowSearchAd(bool) {
        this.ifShowSearchAd = bool;
    }


	// 在普通搜索完成输入点击搜素时更新表单的默认值
    @action setFormFields(value) {
        this.form.updateFields(value, true); // true代表完全覆盖方式更新条件值
    }

    @action
    setLoading(newBool = false) {
        this.loading = newBool;
	}

	@action
	initData = () => {
		// 在初始化数据开始前应该有进度条表示数据正在加载
		this.setLoading(true);
		this.searchKey = '';
		smsRegularApi.getSmsRegularCondition().then(action((data) => {
      const { conditioninfo } = data;
      this.conditioninfo = conditioninfo;
      this.form = new WeaForm();
      // 根据高级搜索条件初始化form
      this.form.initFormFields(conditioninfo);
      this.doSearch();
    }));
	};

	/*
	*暂停发送短信
	*/
	@action stopSmsRegular = params => smsRegularApi.stopSmsRegular(params)

	/*
	*立即发送短信
	*/
	@action sendSmsRegular = params => smsRegularApi.sendSmsRegular(params)

	/*
	*保存批量短信具体人员短信
	*/
	@action saveSmsRegularMemberInfo = params => smsRegularApi.saveSmsRegularMemberInfo(params)

	/*
	*保存批量发送修改内容
	*/
	@action saveSmsRegularInfo = params => smsRegularApi.saveSmsRegularInfo(params)

	/*
	*获取批量发送中具体人员短信
	*/
	@action getSmsRegularMemberInfo = (params) => {
		smsRegularApi.getSmsRegularMemberInfo(params).then((data) => {
			this.receiverNumber = data.receiverNumber;
			this.msg = data.msg;
			this.messageStatus = data.messageStatus;
			this.openRegularMemberInfo();
		});
	}

	/*
	*获取批量发送人员列表
	*/
	@action getSmsRegularMemberList = (params, callback) => {
		smsRegularApi.getSmsRegularMemberList(params).then((data) => {
			this.regularTableStore.getDatas(data.sessionkey, 1);
			this.regularDataKey = data.sessionkey;
			this.sendStatus = data.sendStatus;
			this.receiverType = data.receiverType;
			this.setLoading(false); // 加载完数据清除进度条
			this.openRegularMemberList();
			if (callback) {
				callback();
			}
		});
	}

	/*
	*删除批量发送中具体人员短信
	*/
	@action deleteSmsRegularMemberInfo = params => smsRegularApi.deleteSmsRegularMemberInfo(params)

	/*
	*获取定时发送短信信息
	*/
	@action getSmsRegularInfo = (params) => {
		smsRegularApi.getSmsRegularInfo(params).then((data) => {
			if (data.status) {
				this.msg = data.msg;
				this.sendDateTime = data.sendDateTime;
				this.sendHrm = data.sendHrm;
				this.sendCrm = data.sendCrm;
				this.sendStatus = data.sendStatus;
				this.receiverType = data.receiverType;
				this.openRegularSmsInfo();
			}
		});
	}

	/*
	*获取定时发送短信高级搜索信息
	*/
	@action getSmsRegularCondition = params => smsRegularApi.getSmsRegularCondition(params)

	/*
	*删除定时发送短信
	*/
	@action deleteSmsRegular = params => smsRegularApi.deleteSmsRegular(params)

    @action doSearch = () => {
	    // 在搜索开始前应该有进度条表示数据正在加载
      this.setLoading(true);
      let searchParamsAd = this.form.getFormParams();
      // 提取发送时间的开始结束
      const beginEndDateTime = searchParamsAd['sendBeforeDate,sendAfterDate'] || ['', ''];
      searchParamsAd = { ...searchParamsAd, sendBeforeDate: beginEndDateTime[0], sendAfterDate: beginEndDateTime[1] };

      smsRegularApi.getSmsRegularList(searchParamsAd).then((data) => {
        this.comsWeaTableStore.getDatas(data.sessionkey, 1);
        this.dataKey = data.sessionkey;
        this.setLoading(false); // 加载完数据清除进度条
      });
    }

    // 获取form当前的值
    @action
    getFormParams() {
        return this.form.getFormParams();
    }


    @action
    onEnterSearch = () => {
	    this.doSearch()
      this.setShowSearchAd(false)
    }

    // 用于获取高级搜索中的表单控件
	  @action getSearchs(store) {
		const FormItem = Form.Item;
		// const conditioninfo = adFormdatas.condition;
		let { form, conditioninfo } = store;
		conditioninfo = toJS(conditioninfo);
		const { isFormInit } = form;
		let group = [];
		// 获取表单的值
		const formParams = form.getFormParams();
		// isFormInit 为true，form已经初始化完成
		isFormInit && conditioninfo.map((c) => {
			let items = [];
			c.items.map((fields) => {
				items.push({
					com: (<FormItem
            ecId={`${this && this.props && this.props.ecId || ''}_FormItem@r5yo8p@${(fields.domkey && fields.domkey[0]) ? fields.domkey[0] : 'r5yo8p'}`}
            label={`${fields.label}`}
            labelCol={{ span: `${fields.labelcol}` }}
            wrapperCol={{ span: `${fields.fieldcol}` }}
					>
						{/* 根据 WeaSwitch 获取对应的组件 */}
						<WeaSwitch
              ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@wqp6lh@${(fields.domkey && fields.domkey[0]) ? fields.domkey[0] : 'wqp6lh'}`}
              fieldConfig={fields}
              form={form}
              formParams={formParams}
              onEnterSearch={this.onEnterSearch}
						/>
					</FormItem>),
					colSpan: 1,
				});
			});
			group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@ojb5gq@WeaSearchGroup@ojb5gq`} needTigger={true} showGroup={c.defaultshow} items={items} />);
		});
		return group;
	}

	// 定义tab中点击高级搜索时的最下面的操作按钮集合
	@action getTabButtonsAd(store) {
		return [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@7ezicm@searchBtn`}
  type="primary"
  onClick={() => {
					// 使得高级搜索在点击搜索按钮2后也能跳转到相应的tab页
					store.doSearch();

					store.setShowSearchAd(false);
				}}
			>
				{getLabel(197, '搜索')}
			</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@dtrmzu@resetBtn`} type="ghost" onClick={() => { store.resetAdSearchForm(); }}>{getLabel(2022, '重置')}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@i0sw52@closeBtn`} type="ghost" onClick={() => { store.setShowSearchAd(false); }}>{getLabel(31129, '取消')}</Button>),
		];
	}

	// 打开弹窗
    @action
    openRegularSmsInfo = () => {
        this.regularSmsVisible = true;
    }

    @action
    closeRegularSmsInfo = () => {
        this.regularSmsVisible = false;
	}

	// 打开弹窗
    @action
    openRegularMemberList = () => {
        this.regularMemberListVisible = true;
    }

    @action
    closeRegularMemberList = () => {
        this.regularMemberListVisible = false;
	}

	// 打开弹窗
    @action
    openRegularMemberInfo = () => {
        this.regularMemberInfoVisible = true;
    }

    @action
    closeRegularMemberInfo = () => {
        this.regularMemberInfoVisible = false;
	}

	// 清空高级搜索表单里面所有字段的值
    @action resetAdSearchForm() {
		this.setFormFields({ msg: { value: '' }, timeSag__fromdate__enddate: { value: '1' }, messagestatus: { value: '' }, messagetype: { value: '' } });
    }
}
